package com.qilu.pe.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatEditText;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.dao.bean.AiExplain;
import com.qilu.pe.dao.http.APIRetrofit;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class AiExplainActivity extends Base2Activity {
    private RecyclerView rcv_ai_list;
    private AppCompatEditText et_item_name, et_abridge;
    private TextView tv_reset, tv_ok;

    private String itemName = "";
    private String abridge = "";

    private AiListAdapter adapter;

    private List<AiExplain> aiExplainList = new ArrayList<>();

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_ai_explain;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rcv_ai_list = findViewById(R.id.rcv_ai_list);
        et_item_name = findViewById(R.id.et_item_name);
        et_abridge = findViewById(R.id.et_abridge);
        tv_reset = findViewById(R.id.tv_reset);
        tv_ok = findViewById(R.id.tv_ok);

        adapter = new AiListAdapter(this);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        rcv_ai_list.setLayoutManager(manager);
        rcv_ai_list.setAdapter(adapter);

        setHeadTitle("AI解读");

        setListeners();
    }

    private void setListeners() {
        setOnClickListener(tv_reset, tv_ok);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_reset) {
            et_item_name.setText("");
            et_abridge.setText("");
        }
        if (v == tv_ok) {
            itemName = et_item_name.getText().toString().trim();
            abridge = et_abridge.getText().toString().trim();
            hideProgress();
            reqSearchAiList();
            showProgress();
        }
    }

    // 搜索ai解读
    private Disposable reqSearchAiList() {
        return APIRetrofit.getDefault().reqAiSearchList(itemName, abridge)
                .compose(RxUtil.<BaseResult2<List<AiExplain>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<AiExplain>>>() {
                    @Override
                    public void accept(BaseResult2<List<AiExplain>> listBaseResult2) throws Exception {
                        hideProgress();
                        if (listBaseResult2.isSuccess()) {
                            aiExplainList = listBaseResult2.getData();
                            adapter.setDataList(aiExplainList);
                        } else {
                            ToastUtils.showShort(listBaseResult2.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }

    private class AiListAdapter extends ListBaseAdapter<AiExplain> {

        public AiListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_ai_search_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_name = holder.getView(R.id.tv_name);
            TextView tv_abridge = holder.getView(R.id.tv_abridge);
            TextView tv_range = holder.getView(R.id.tv_range);
            TextView tv_sig = holder.getView(R.id.tv_sig);

            AiExplain ai = mDataList.get(position);
            tv_name.setText(ai.getName());
            tv_abridge.setText(ai.getEnglish());
            tv_range.setText(ai.getScope());
            tv_sig.setText(ai.getSignificance());
        }
    }
}